﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StockMatch.WebSite
{
    public class DomainConfig : ConfigurationSection
    {
        /// <summary>
        /// 键值对配置
        /// </summary>
        [ConfigurationProperty("routes")]
        [ConfigurationCollection(typeof(DomainRouteSection))]
        public RoutesCollection Routes
        {
            get { return (RoutesCollection)this["routes"]; }
            set { this["routes"] = value; }
        }

        public static DomainRouteSection GetDomainRouteConfig(string name)
        {
            return ((DomainConfig)ConfigurationManager.GetSection("siteRoutes")).Routes[name];
        }
    }

    public class DomainRouteSection : ConfigurationSection
    {
        /// <summary>
        /// 注册route的name
        /// </summary>
        [ConfigurationProperty("name", IsRequired = true)]
        public string name
        {
            get { return Convert.ToString(this["name"]); }
            set { this["name"] = value; }
        }

        /// <summary>
        /// 注册route的url
        /// </summary>
        [ConfigurationProperty("url", IsRequired = true)]
        public string url
        {
            get { return Convert.ToString(this["url"]); }
            set { this["url"] = value; }
        }

        /// <summary>
        /// 注册route的domain
        /// </summary>
        [ConfigurationProperty("domain")]
        public string domain
        {
            get { return Convert.ToString(this["domain"]); }
            set { this["domain"] = value; }
        }

        /// <summary>
        /// 域
        /// </summary>
        [ConfigurationProperty("areas")]
        public string areas
        {
            get { return Convert.ToString(this["areas"]); }
            set { this["areas"] = value; }
        }

        /// <summary>
        /// 命名空间
        /// </summary>
        [ConfigurationProperty("Namespace")]
        public string Namespace
        {
            get { return Convert.ToString(this["Namespace"]); }
            set { this["Namespace"] = value; }
        }
    }

    public class RoutesCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new DomainRouteSection();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((DomainRouteSection)element).name.ToLower();
        }

        /// <summary>
        /// 获取指定名称的DomainRoute配置
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public new DomainRouteSection this[string name]
        {
            get
            {
                return (DomainRouteSection)base.BaseGet(name.ToLower());
            }
        }
    }

    public static class GlobalConfig
    {
        /// <summary>
        /// 版本信息(机电在线)
        /// </summary>
        public static string Version
        {
            get
            {
                return ConfigurationManager.AppSettings["Version"];
            }
        }
        

        /// <summary>
        /// 站点名称(机电在线)
        /// </summary>
        public static string SiteName
        {
            get
            {
                return ConfigurationManager.AppSettings["siteName"];
            }
        }
        /// <summary>
        /// 主站地址(http://www.jdol.com.cn)
        /// </summary>
        public static string Domain
        {
            get
            {
                return ConfigurationManager.AppSettings["Domain"];
            }
        }

       

        /// <summary>
        /// cookie 关联的域
        /// </summary>
        public static string CookieDomain
        {
            get
            {
                return ConfigurationManager.AppSettings["CookieDomain"];
            }
        }

        /// <summary>
        /// 页面数
        /// </summary>
        public static string PageSize
        {
            get
            {
                return ConfigurationManager.AppSettings["PageSize"].ToString();
            }
        }


        
        /// <summary>
        /// 产品地址
        /// </summary>
        public static string Product
        {
            get
            {
                return ConfigurationManager.AppSettings["Product"].ToString();
            }
        }


        /// <summary>
        /// 文件上传路径
        /// </summary>
        public static string FileUploadPath
        {
            get { return ConfigurationManager.AppSettings["FileUploadPath"].ToString(); }
        }

        /// <summary>
        /// 文件访问路径
        /// </summary>
        public static string UploadFileDomain
        {
            get { return ConfigurationManager.AppSettings["UploadFileDomain"].ToString(); }
        }

    }

    public static class AreasConfig
    {
        /// <summary>
        /// 主站
        /// </summary>
        public static string Web
        {
            get
            {
                return DomainConfig.GetDomainRouteConfig("Web") == null ? "NOT" : DomainConfig.GetDomainRouteConfig("Web").areas.ToString();
            }
        }
        
        /// <summary>
        ///后台管理
        /// </summary>
        public static string Admin
        {
            get { return DomainConfig.GetDomainRouteConfig("Admin") == null ? "NOT" : DomainConfig.GetDomainRouteConfig("Admin").areas.ToString(); }
        }

        public static string AdminSite
        {
            get { return DomainConfig.GetDomainRouteConfig("AdminSite") == null ? "NOT" : DomainConfig.GetDomainRouteConfig("AdminSite").areas.ToString(); }
        }
    }
}
